package com.lagou.mapper;

import com.lagou.domain.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @Author wsl
 * @Date 2021/11/4 20:25
 * @Project_Name mybatis_quickstart
 */

@CacheNamespace //表示开启了二级缓存
public interface UserMapper {

    /**
     * 1.查询操作
     * @return
     */
    @Select("select * from user")
    public List<User> findAll();

    /**
     * 2.添加操作
     * @param user
     */
    @Insert("insert into user(username,birthday,sex,address) values(#{username},#{birthday},#{sex},#{address})")
    public void save(User user);

    /**
     * 3.修改操作
     * @param user
     */
    @Update("update user set username=#{username},birthday=#{birthday} where id=#{id}")
    public void update(User user);

    /**
     * 4.删除操作
     * @param id
     */
    @Delete("delete from user where id=#{id}")
    public void delete(Integer id);

    /**
     * 5.用于一对一查询: 根据用户id查询用户信息
     * @param uid
     * @return
     */
    @Select("select * from user where id = #{uid}")
    public User findById(Integer uid);

    /**
     * 6.一对多查询：查询所有用户，与此同时查询每个用户所属的订单信息 (注解方式)
     * @return
     */
    @Select("select * from user")
    @Results({
            @Result(property = "id",column = "id",id = true),
            @Result(property = "username",column = "username"),
            @Result(property = "birthday",column = "birthday"),
            @Result(property = "sex",column = "sex"),
            @Result(property = "address",column = "address"),
             @Result(property = "ordersList",javaType = List.class,column = "id",many = @Many(select = "com.lagou.mapper.OrderMapper.findOrderByUid")),
    })
    public List<User> findAllWithOrder();

    /**
     * 7.多对多查询：查询所有用户，与此同时查询出每个用户所属的角色列表 (注解方式)
     * @return
     */
    @Select("select * from user")
    @Results({
            @Result(property = "id",column = "id",id = true),
            @Result(property = "username",column = "username"),
            @Result(property = "birthday",column = "birthday"),
            @Result(property = "sex",column = "sex"),
            @Result(property = "address",column = "address"),
             @Result(property = "roleList",javaType = List.class,column = "id",many = @Many(select = "com.lagou.mapper.RoleMapper.findRoleByUid"))
    })
    public List<User> findAllWithRole();
}
